package uk.ac.strath.cis.metric_spaces.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Robert
 *
 */
public interface Resolver {

    /**
     * @param i
     * @return
     */
    String lookup(final Integer i);

    /**
     * @param s
     * @return
     */
    int lookup(final String s);
    
    static final Resolver instance = new HashResolver();
    
    /**
     * A two-way singleton dictionary.
     */
    class HashResolver implements Resolver {

        
        private final Map<String, Integer> dictionary = new HashMap<String, Integer>();
        private final Map<Integer, String> inverse = new HashMap<Integer, String>();
        private static int counter = 1;

        private HashResolver() { }

        public int lookup(final String s) {// one way
            Integer l = dictionary.get(s);
            if (l == null) {
                l = ++counter;
                dictionary.put(s, counter);
                inverse.put(counter, s);
            }
            return l;
        }

        public String lookup(final Integer i) {
            return inverse.get(i);
        }
    }
}
